import './css/captcha.less';

const captcha = (function () {
    let $captcha;
    const device = 'mobile';
    let isReady = false;
    let callback = '';
    let closeCallBack = '';
    function ready() {
        if (!$captcha) {
            $captcha = $(
                '<div class="captcha">'
                    + '<div class="captchaMask"></div>'
                    + '<div class="popup-captcha-mobile"></div>'
                    + '</div>'
            );
            $captcha.appendTo('body');
            $captcha.find('.captchaMask').click(() => {
                $captcha.hide();
                $.isFunction(closeCallBack) && closeCallBack();
            });
        }
        const handlerPopupMobile = function (captchaObj) {
            $captcha.find('.popup-captcha-mobile').html('');
            // 将验证码加到id为captcha的元素里
            captchaObj.appendTo('.popup-captcha-mobile');
            // 拖动验证成功后两秒(可自行设置时间)自动发生跳转等行为
            captchaObj.onSuccess(() => {
                const validate = captchaObj.getValidate();
                setTimeout(() => {
                    $captcha.hide();
                    // 进行二次验证
                    WPT.postData(
                        '//api.weipaitang.com/weibo/v1.0/captcha/verify',
                        {
                            type: device,
                            geetest_challenge: validate.geetest_challenge,
                            geetest_validate: validate.geetest_validate,
                            geetest_seccode: validate.geetest_seccode,
                        },
                        res => {
                            if (res.code !== 0) {
                                WPT.Modal.alert(res.msg);
                                return;
                            }
                            if (res.data.status === 'success') {
                                $.isFunction(callback) && callback();
                            }
                        }
                    );
                }, 400);
            });
        };
        WPT.getData(
            '//api.weipaitang.com/weibo/v1.0/captcha/init',
            {
                type: device,
                t: Date.now(), // 加随机数防止缓存
            },
            res => {
                if (res.code !== 0) {
                    WPT.Modal.alert(res.msg);
                    return;
                }
                isReady = true;
                // 使用initGeetest接口
                // 参数1：配置参数
                // 参数2：回调，回调的第一个参数验证码对象，之后可以使用它做appendTo之类的事件
                const { gt, challenge, success } = res.data;
                window.initGeetest(
                    {
                        gt,
                        challenge,
                        width: WPT.$curPage.width(),
                        offline: !success, // 表示用户后台检测极验服务器是否宕机，一般不需要关注
                    },
                    handlerPopupMobile
                );
            }
        );
    }

    function open(success, close) {
        if (isReady) {
            $captcha.show();
            callback = success;
            closeCallBack = close;
        }
        return isReady;
    }

    function init() {
        WPT.loadScript('//static.geetest.com/static/tools/gt.js', () => {
            ready();
        });
    }
    return {
        init,
        open,
    };
}());

export default captcha;
